package com.gaolang.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaolang.web.entities.ControllerEndpoint;
import com.gaolang.web.entities.Department;
import com.gaolang.web.handler.BusinessException;
import com.gaolang.web.response.Result;
import com.gaolang.web.response.ResultCode;
import com.gaolang.web.service.IDepartmentService;
import com.wuwenze.poi.ExcelKit;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author gaolang
 * @since 2021-02-23
 */
@RestController
@RequestMapping("/department")
@Api(value = "系统部门模块",tags = "系统部门接口")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    @GetMapping("/findDeptAndCount")
    @ApiOperation(value = "查询部门及人数",notes = "从部门表中分组查询用户人数")
    public Result findDeptAndCount(){
        List<Department> departments = departmentService.findDeptAndCount();
        if (departments.size() == 0) {
            throw new BusinessException(ResultCode.DEPARTMENT_NOT_EXIST.getCode(),ResultCode.DEPARTMENT_NOT_EXIST.getMessage());
        }

        return Result.ok().data("departments",departments);
    }

    @GetMapping("/deptList")
    @ApiOperation(value = "部门列表",notes = "部门列表")
    public Result deptList(@RequestParam(required = true,defaultValue = "1")Integer current
            ,@RequestParam(required = true,defaultValue = "6")Integer size,@RequestParam(required = false) String name){
        Page<Department> page = new Page<>(current,size);
        QueryWrapper<Department> wrapper = null;
        if (!Objects.isNull(name) && !Objects.equals(name.trim(),"")) {
            wrapper = new QueryWrapper<>();
            wrapper.like("name",name.trim());
        }
        Page<Department> page2 = departmentService.page(page, wrapper);
        List<Department> department = departmentService.deptList();
        for (Department record : page2.getRecords()) {
            for (Department dept : department) {
                if (record.getId().equals(dept.getId())){
                    record.setDeptCount(dept.getDeptCount());
                    break;
                }
            }
        }

        return Result.ok().data("total",page2.getTotal()).data("records",page2.getRecords());
    }


    @PostMapping("/addDept")
    @ApiOperation(value = "添加部门",notes = "添加部门信息")
    public Result add(@RequestBody Department department){
        department.setCreateTime(new Date());
        department.setModifiedTime(new Date());
        departmentService.save(department);
        return Result.ok();
    }

    @PostMapping("/deleteById")
    @ApiOperation(value = "删除部门",notes = "删除部门")
    public Result delete(Long id){
        departmentService.removeById(id);
        return Result.ok();
    }

    /**
     * 导出excel
     * @param response
     */
    @ApiOperation(value = "导出excel", notes = "导出所有部门的excel表格")
    @PostMapping("/excel")
    @ControllerEndpoint(exceptionMessage = "导出Excel失败",operation = "导出部门excel")
    public void export(HttpServletResponse response) {
        List<Department> departments = departmentService.list();
        ExcelKit.$Export(Department.class, response).downXlsx(departments, false);
    }


}
